var ISubscribe_Admin = {
        curCurrency : 'create-currency',


        toggleAddUser : function(id){
            var el = document.getElementById('ISubscribeAddUser_'+id);
            if(!el) return;
            if(el.style.display == 'none'){
                el.style.display = 'block';
            }else{
                el.style.display = 'none';
            }
            document.getElementById('ISubscribeViewUsers_'+id).style.display = 'none';
        },

        toggleViewUsers : function(id){
            var el = document.getElementById('ISubscribeViewUsers_'+id);
            if(!el) return;
            if(el.style.display == 'none'){
                el.style.display = 'block';
            }else{
                el.style.display = 'none';
            }
            document.getElementById('ISubscribeAddUser_'+id).style.display = 'none';
        },


	addPrice : function(){
		var form = document.getElementById('new_form');
		var fs = document.getElementById('fs1');
		var numPrices = form.getElementsByTagName('input')['numPrices'];
		var priceNum = numPrices.value;
		numPrices.value++;
		var currencies = document.getElementById('currenciesDummy');
		var groups = document.getElementById('groupsDummy');
		var newCurrencies = currencies.cloneNode(true);
		var newGroups = groups.cloneNode(true);
		newCurrencies.name = 'cur_'+priceNum;
		newGroups.name = 'group_'+priceNum;
		var component = document.createElement('div');
		component.className = 'component';
		component.id = 'price_'+priceNum+'_component';
		var aftertext = document.createElement('div');
		aftertext.className = 'aftertext';
		var price = document.createElement('input');
		price.type = 'text';
		price.name = 'price_'+priceNum;
		var label = document.createElement('label');
		label.innerHTML = 'Price ';
		var span = document.createElement('span');
		var span2 = document.createElement('span');
		var delLink = document.createElement('a');
		delLink.href = 'javascript: ISubscribe_Admin.delPrice('+priceNum+')';
		delImg = document.createElement('img');
		delImg.alt = 'Delete';
		delImg.src = "themes/NCleanGrey/images/icons/system/delete.gif";
		delImg.title = 'Delete';
		delImg.className = 'systemicon';
		delLink.appendChild(delImg);
		span2.appendChild(delLink);
		component.appendChild(label);
		component.appendChild(price);
		span.appendChild(newCurrencies);
		span.appendChild(newGroups);
		aftertext.appendChild(span);
		aftertext.appendChild(span2);
		component.appendChild(aftertext);;
		component.style.display = "none";
		fs.appendChild(component);
		Effect.SlideDown(component);
		Effect.Appear(component);
        
	},


    showCurrencies : function(id){
        if(!document.getElementById(id)) return;
        
        if(id == this.curCurrency) return;
        
        Effect.Fade(this.curCurrency);
        
        setTimeout('ISubscribe_Admin.displayCurrency(\''+id+'\')', 800);
        
    },

    displayCurrency : function (id){
        
        document.getElementById(this.curCurrency).style.display = 'none';
        
        Effect.Appear(id);
        
        this.curCurrency = id;
        
    },


    delPrice : function(num){
	   var id = 'price_'+num+'_component';
		if(!document.getElementById(id)) return;
		var component = document.getElementById(id);
		Effect.Fade(component);
        setTimeout('ISubscribe_Admin.removeComponent("'+id+'")', 800);
        var form = document.getElementById('new_form');
	},
	
	removeComponent : function(id){
        var component = document.getElementById(id);
        component.parentNode.removeChild(component);
    },

	toggleGroup : function(selObj, textareaId){
		if(document.getElementById(selObj)) var selObj = document.getElementById(selObj);
        if(selObj.options && selObj.options[selObj.selectedIndex])
        var key = selObj.options[selObj.selectedIndex].value;

        if(!document.getElementById(textareaId)) return;
        var txt = document.getElementById(textareaId);

        if(key == 'public') return;

        if(txt.value.match(key)){
            // going to remove a group from the list
            txt.value = txt.value.replace(key, '');
        }else{
            // adding a group to the list
            if(txt.value == '') txt.value = key;
            else txt.value = txt.value+','+key;
        };
        // make sure we don't have a comma at the start and end of the list
        if(txt.value.substring(0, 1) == ',')
            txt.value = txt.value.substring(1);
        if(txt.value.substring((txt.value.length-1))==',')
            txt.value = txt.value.substring(0, txt.value.length-1);
        // warn them that having no groups cannot be done
        if(txt.value == '') alert('Make sure your users are getting something for their money ;)');

	}

}







function showErrorMsg(msg, rootUrl){
    var cont = document.getElementById('MainContent');
    var pCont = document.getElementsByClassName('pagecontainer');
    pCont = pCont[0];
    var objs = document.getElementsByClassName('navt_menu');
    var navt = objs[0];
    var div = document.createElement('div');
    var div2 = document.createElement('div');
    var img = document.createElement('img');
    var span = document.createElement('span');
    span.innerHTML = ' '+msg;
    img.src = rootUrl+'/admin/themes/default/images/icons/system/stop.gif';
    img.alt = 'Error';
    img.title = 'Error';
    img.className = 'systemicon';
    div.className = 'pageerrorcontainer';
    div.id = 'errorMsg';
    div2.className = 'pageoverflow';
    div2.appendChild(img);
    div2.appendChild(span);
    div.appendChild(div2);
    appendAfter(navt, div);
}

function showSuccessMsg(msg, rootUrl){

    var cont = document.getElementById('MainContent');
    var pCont = document.getElementsByClassName('pagecontainer');
    pCont = pCont[0];
    var objs = document.getElementsByClassName('navt_menu');
    var navt = objs[0];
    var div = document.createElement('div');
    var p = document.createElement('p');
    var img = document.createElement('img');
    var span = document.createElement('span');
    span.innerHTML = ' '+msg;
    img.src = rootUrl+'/admin/themes/default/images/icons/system/accept.gif';
    img.alt = 'Success';
    img.title = 'Success';
    img.className = 'systemicon';
    div.className = 'pagemcontainer';
    div.id = 'successMsg';
    p.className = 'pagemessage';
    p.appendChild(img);
    p.appendChild(span);
    div.appendChild(p);
    appendAfter(navt, div);
}

// function derived from http://book.itzero.com/read/others/McGraw.Hill.Osborne.JavaScript.2.0.The.Complete.Reference.Second.Edition.eBook-LiB_html/8166final/LiB0073.html
function appendAfter(existingNode, newNode)
{
 if (existingNode.parentNode)
  {
    if (existingNode.nextSibling)
       existingNode.parentNode.insertBefore(newNode, existingNode.nextSibling);
    else
       existingNode.parentNode.appendChild(newNode);
  }
}
